package com.itany.cloud.controller;

import com.itany.cloud.entity.User;
import com.itany.cloud.util.ResponseResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {

    static Map<Integer, User> users = new HashMap<>();
    static Integer id = 1;

    @GetMapping
    public ResponseResult findAll(){
        List<User> list = new ArrayList<>(users.values());
        ResponseResult result = new ResponseResult();
        result.setResponseCode("1001");
        result.setMessage("成功");
        result.setReturnObject(list);
        return result;
    }

    @PostMapping
    public ResponseResult addUser(User user){
        user.setId(id++);
        users.put(user.getId(),user);
        return ResponseResult.success();
    }

    @GetMapping("/{id}")
    public ResponseResult findById(@PathVariable Integer id){
//        User user = users.get(id);
        User user = new User(9002,"9002","9002","9002","9002");
        return ResponseResult.success(user);
    }

    @PutMapping
    public ResponseResult modifyUser(User user){
        User u = users.get(user.getId());
        u.setUsername(user.getUsername());
        u.setPassword(user.getPassword());
        u.setPhone(user.getPhone());
        u.setAddress(user.getAddress());
        return ResponseResult.success();
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteUser(@PathVariable Integer id){
        users.remove(id);
        return ResponseResult.success();
    }

}
